$(function () {
    window.islogin = $('#doc').data('islogin');
    var getParams = getArgs();
    var currentHotel = getParams.hotel;
    toastr.options = {
        "newestOnTop": false,
        "timeOut": 1500,
        "extendedTimeOut": 0,
        "escapeHtml": true,
        "positionClass": "toast-bottom-center"
    };
    if (currentHotel === undefined || currentHotel === '') {
        toastr.error(lang('toastr.grogshop'));
        return;
    }     
    var couponType = ['',lang('Coupon.01'),lang('Coupon.02')];
    var requestCoupon = function () {
        toggleLoading(true);
        ajaxRequest('/Home/Hotel/getCoupon', {
            "hotel": currentHotel
        }, 'post', function (data) {
            toggleLoading(false);
            var info = data.data;
            var couponHTML = '';
            if (info.length === 0) {
                couponHTML = $('#templateCouponEmpty').clone().html();
            } else {
                $.each(info, function (key, value) {
                    var templateCouponHTML = $($('#templateCouponList').clone().html());
                    templateCouponHTML.find('.coupon-data').text(value.couponStart+'-'+value.couponEnd);
                    templateCouponHTML.find('.coupon-type').text(couponType[value.couponType]);
                    templateCouponHTML.find('.coupon-name .info').text(value.couponName);
                    templateCouponHTML.find('.coupon-desc').text(lang('Coupon.03')+value.couponPrice+lang('Coupon.04'));
                    templateCouponHTML.find('.coupon-detail').attr({'data-no': value.couponNo,'data-current': value.hotelId});
                    templateCouponHTML.find('.coupon-use').text(value.receiveType);
                    templateCouponHTML.find('.coupon-draw').attr({
                        'data-no':value.couponNo,
                        'data-value':value.status,
                        'data-type':value.couponType,
                        'data-price':value.couponMinusPrice,
                        'data-discount':value.couponDiscount,
                    });
                    if(value.status == 2){
                        templateCouponHTML.find('.couponback').removeClass('coupon-draw');
                    }
                    if(value.couponType == 1){
                        templateCouponHTML.find('.coupon-price').removeClass('hidden').find('span').text(value.couponMinusPrice);
                    }else{
                        templateCouponHTML.find('.coupon-discount').removeClass('hidden').find('span').text(value.couponDiscount);
                    }
                    couponHTML += templateCouponHTML.prop('outerHTML');
                });
            }
            $('#coupon-list').html(couponHTML);
        }, function(error){
            toggleLoading(false); 
            toastr.error(typeof error.responseJSON === 'undefined' ? lang('tip.server') : error.responseJSON.info);
        });        
    }
    requestCoupon();
    $('#doc').on('click', '.coupon-refresh', function (e) {
        e.preventDefault();
        requestCoupon();
    });
    $('#coupon-list').on('click','.coupon-detail',function(e){
       e.stopPropagation(); 
       var couponNo = $(this).data('no');
       var currentHotel = $(this).data('current');
       window.location.href = "/Home/CouponPage/coupondetail?hotel=" + currentHotel+'&no='+couponNo;
    });    
    var importModal = $('#importModal');
    var getimportModal = function(){
        importModal.find('.coupon-price').addClass('hidden');
        importModal.find('.coupon-discount').addClass('hidden');
        if(window.couponType == 1){
            importModal.find('.coupon-price').removeClass('hidden').find('span').text(window.couponMinusPrice);
        }else{
            importModal.find('.coupon-discount').removeClass('hidden').find('span').text(window.couponDiscount);
        }                
        importModal.removeClass('hidden');        
    };
    $('#coupon-list').on('click','.coupon-draw[data-value=1]',function(e){
        e.stopPropagation(); 
        window.couponNo = $(this).data('no');
        window.couponType = $(this).data('type');
        window.couponMinusPrice = $(this).data('price');
        window.couponDiscount = $(this).data('discount');
        if(window.islogin != 0){
            ajaxRequest('/Home/Coupon/addUserCoupon', {
                "hotel": currentHotel,
                "couponNo":window.couponNo
            }, 'post', function (data) {
                requestCoupon();
                layer.msg(lang('Coupon.05'),{time:1000});
            }, function(error){
                if(window.islogin != ''){
                    toastr.error(typeof error.responseJSON === 'undefined' ? lang('tip.server') : error.responseJSON.info);
                }else{
                    getimportModal();
                }
            });              
        }else{
            getimportModal();
        }              
    });
    importModal.on('click','.coupon-close',function(e){
        e.stopPropagation(); 
        importModal.addClass('hidden');
    });
    importModal.on('click','.getCode', function (e) {
        var submitBtn = $(this);
        var params = {};
        params.mobile = importModal.find('.requestMobile').val();
        params.areaCode = importModal.find('.areaCodeValue').text().trim();
        if(params.mobile == ''){
            toastr.error(lang('tip.mobileWrite'))
            return false;
        }
        ajaxRequest('/Home/Public/smsCouponLoginVcode', params, 'post', function (data) {
            timeDisplay(submitBtn, 60);
        }, function (error) {
            toastr.error(typeof error.responseJSON === 'undefined' ? lang('tip.server') : error.responseJSON.info);
        });
    });  
    importModal.on('click','.coupon-receive span',function(e){
        e.stopPropagation(); 
        var params = {};
        var ret = /^\d{4}$/;
        params.hotel = currentHotel;
        params.couponNo = window.couponNo;
        params.mobile = importModal.find('.requestMobile').val();
        params.areaCode = importModal.find('.areaCodeValue').text().trim();
        params.vcode = importModal.find('.requestCode').val();
        if(params.mobile == ''){
            toastr.error(lang('tip.mobileWrite'))
            return false;
        }
        if(params.vcode == ''){
            toastr.error(lang('tip.messageNull'))
            return false;
        }
        if(!ret.test(params.vcode)){
            toastr.error(lang('tip.message'))
            return false;
        }
        ajaxRequest('/Home/Public/getCouponAndSmsLogin',params, 'post', function (data) {
            importModal.addClass('hidden');
            requestCoupon();
            layer.msg(lang('Coupon.05'),{time:1000});
            window.islogin = 2;
        }, function(error){
            toastr.error(typeof error.responseJSON === 'undefined' ? lang('tip.server') : error.responseJSON.info);
        });  
    });
});
